<template>
  <a-dropdown placement="bottomRight">
    <span class="ant-pro-account-avatar">
      <a-avatar size="small" :src="avatar" class="antd-pro-global-header-index-avatar" />
    </span>
  </a-dropdown>
</template>

<script>
import { Modal } from 'ant-design-vue'
import {getUserProfile} from "@/api/api";

export default {
  name: 'AvatarDropdown',
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      userInfo: {},
      avatar: ''
    }
  },
  created() {
    getUserProfile().then(res =>  {
      console.log('用户信息',res)
      this.userInfo = {
        id: res?.user?.id,
        ref_team_id:  res?.user?.slack_user?.ref_team_id,
        ref_user_id: res?.user?.slack_user?.ref_user_id,
        profile: res?.user?.slack_user?.profile || {}
      }
      this.avatar = res?.user?.slack_user?.profile?.image_48
      this.$store.commit('SET_AVATAR', this.avatar)
      this.$store.commit('SET_INFO', this.userInfo)

    })
  },
  methods: {
    handleToCenter () {
      this.$router.push({ path: '/account/center' })
    },
    handleToSettings () {
      this.$router.push({ path: '/account/settings' })
    },
    handleLogout (e) {
      Modal.confirm({
        title: this.$t('layouts.usermenu.dialog.title'),
        content: this.$t('layouts.usermenu.dialog.content'),
        onOk: () => {
          // return new Promise((resolve, reject) => {
          //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1500)
          // }).catch(() => console.log('Oops errors!'))
          return this.$store.dispatch('Logout').then(() => {
            this.$router.push({ name: 'login' })
          })
        },
        onCancel () {}
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  /deep/ .action {
    margin-right: 8px;
  }
  /deep/ .ant-dropdown-menu-item {
    min-width: 160px;
  }
}
</style>
